import {ElMessage, ElMessageBox} from "element-plus";


export default {
    async submitAdd(formEl, param, method, callback) {
        await formEl.validate((valid, fields) => {
            if (!valid) {
                ElMessage({
                    showClose: true,
                    message: "校验失败，请检查输入项！",
                    type: "error",
                });
                return;
            }
            method.call(this, param).then((res) => {
                if (res.status.status_code !== 0) {
                    ElMessage({
                        showClose: true,
                        message: res.status.status_reason,
                        type: "error",
                    });
                    return;
                }
                ElMessage({
                    showClose: true,
                    message: res.data ? "添加成功！" : "添加失败！",
                    type: res.data ? "success" : "error",
                });
                if (res.data) {
                    callback();
                }
            }).catch((err) => {
                ElMessage({
                    showClose: true,
                    message: err,
                    type: "error",
                });
            });

        });
    },
    async submitUpdate(formEl, param, method, callback) {
        await formEl.validate((valid, fields) => {
            if (!valid) {
                ElMessage({
                    showClose: true,
                    message: "校验失败，请检查输入项！",
                    type: "error",
                });
                return;
            }
            method.call(this, param).then((res) => {
                console.info(res, "----------");
                if (res.status.status_code !== 0) {
                    ElMessage({
                        showClose: true,
                        message: res.status.status_reason,
                        type: "error",
                    });
                    return;
                }
                ElMessage({
                    showClose: true,
                    message: res.data ? "修改成功！" : "修改失败！",
                    type: res.data ? "success" : "error",
                });
                if (res.data) {
                    callback();
                }
            }).catch((err) => {
                ElMessage({
                    showClose: true,
                    message: err,
                    type: "error",
                });
            });

        });
    },
    handleDelete(param, method, callback) {
        if (param.shopId != null) {
            ElMessage({
                showClose: true,
                message: "已绑定店铺不允许删除，请先解绑",
                type: "error",
            });
            return;
        }
        ElMessageBox.confirm('确定删除? id=' + param.id)
            .then(() => {
                method.call(this, param).then((res) => {
                    ElMessage({
                        showClose: true,
                        message: res.data ? "删除成功！" : "删除失败！",
                        type: res.data ? "success" : "error",
                    });
                    if (res.data) {
                        callback();
                    }
                }).catch((err) => {
                    ElMessage({
                        showClose: true,
                        message: err,
                        type: "error",
                    });
                });
            });
    },
    doHandle(name, param, method, callback) {
        ElMessageBox.confirm('确定' + name + '? id=' + param.id)
            .then(() => {
                method.call(this, param).then((res) => {
                    ElMessage({
                        showClose: true,
                        message: res.data ? name + "成功！" : name + "失败！",
                        type: res.data ? "success" : "error",
                    });
                    if (res.data) {
                        if (callback) callback();
                    }
                }).catch((err) => {
                    ElMessage({
                        showClose: true,
                        message: err,
                        type: "error",
                    });
                });
            });
    }
}